﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using HRMSys.Model;
using HRMSys.DAL;

namespace HRMSys.UI.SystemMgr
{
    /// <summary>
    /// OperatorListUI.xaml 的交互逻辑
    /// </summary>
    public partial class OperatorListUI : Window
    {
        public OperatorListUI()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.LoadData();
        }

        private void LoadData() 
        {
            OperatorDAL dal = new OperatorDAL();
            Operator[] ops = dal.GetAll();
            gridOperators.ItemsSource = ops;
        }

        private void BtnAdd_Click(object sender, RoutedEventArgs e)
        {
            OperatorEditUI ui = new OperatorEditUI();
            ui.IsInsert = true;
            if (ui.ShowDialog() == true) 
            {
                OperatorDAL dal = new OperatorDAL();
                Operator[] ops =  dal.GetAll();
                gridOperators.ItemsSource = ops;
            }
        }

        private void BtnEdit_Click(object sender, RoutedEventArgs e)
        {
            object selectedItem = gridOperators.SelectedItem;
            if (selectedItem == null)
            {
                MessageBox.Show("请选择要编辑的数据!");
            }
            else
            {
                OperatorEditUI ui = new OperatorEditUI();
                ui.IsInsert = false;
                ui.EditingId = ((Operator)selectedItem).Id;

                if (ui.ShowDialog() == true)
                {
                    this.LoadData();
                }
            }
        }

        private void BtnDelete_Click(object sender, RoutedEventArgs e)
        {
            object selectedItem = gridOperators.SelectedItem;
            if (selectedItem == null)
            {
                MessageBox.Show("请选择要删除的数据!");
            }
            else 
            {
                MessageBoxResult mbrResult = MessageBox.Show("是否确定删除该人员?", "删除确认",
                    MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No);
                if (mbrResult == MessageBoxResult.Yes) 
                {
                    Guid id = ((Operator)selectedItem).Id;
                    OperatorDAL dal = new OperatorDAL();
                    int result = dal.DeleteById(id);
                    if (result==1)
                    {
                        MessageBox.Show("删除成功!", "系统提示", MessageBoxButton.OK);
                        //删除成功后, 记录操作日志
                        Guid operatorId = CommonHelper.GetOperatoId();
                        (new OperationDAL()).Insert(operatorId, "删除管理员" + ((Operator)selectedItem).UserName);
                    }

                   

                    this.LoadData();
                }
            }
        }
    }
}
